home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261ini.zip / PSTOPPM.PS < prev    next >
Text File  |  1992-09-15  |  8KB  |  220 lines

  1. %! PS-Adobe-2.0
  2. %% Program for reading a .ps file and writing out a PPM file.
  3. %% For Ghostscript 2.5.2.
  4. %%
  5. %% Modified by L. Peter Deutsch 9/10/92:
  6. %%   internal procedures didn't use `bind';
  7. %%   grestoreall undid selection of PPM device.
  8. %% Modified by L. Peter Deutsch 4/6/92:
  9. %%   Ghostscript 2.4 requires all 8 primary colors to be in the palette.
  10. %% Modified by L. Peter Deutsch 1/17/92:
  11. %%   the palette for makeimagedevice is now a string, not an array.
  12. %% Modified by L. Peter Deutsch 9/24/91:
  13. %%   allow starting page number to be specified.
  14. %% Modified by L. Peter Deutsch 7/7/91 to keep track of page count
  15. %%   in a way that gets around save and restore.
  16. %% Modified by L. Peter Deutsch 11/07/90
  17. %%   to use filename.ppm for the first page, renamed to filename.1ppm
  18. %%   with subsequent pages .2ppm, etc. if more than one page.
  19. %% Modified by Henry Minsky 11/03/90
  20. %%   for each showpage, it writes out a ppm file with name filename.ppm.N
  21. %%   where N increments each showpage, starting at 1
  22. %% Modified by L. Peter Deutsch -- Aladdin Enterprises -- 08/25/90 --
  23. %%   converted from a one-shot program to a utility package,
  24. %%   designed to be used from an interactive terminal.
  25. %% Modified by L. Peter Deutsch -- Aladdin Enterprises -- 08/02/90
  26. %% Modified on 08/02/90 for using the CORRECT color map.
  27. %% Modified 06/26/90 for a color file
  28. %% Original version by Phillip Conrad - Perfect Byte, Inc.
  29. %%
  30. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  31.  
  32. %%%%%% Define the directory for holding the PPM information
  33.  
  34. /ppmdict 30 dict def
  35. ppmdict begin
  36.  
  37.   /Horz_dpi 72 def        % defaults to screen density
  38.   /Vert_dpi 72 def
  39.  
  40.   /OutFilePrefix () def        % default to null (current directory)
  41.  
  42.   /FirstPageNumber 1 def    % any non-negative integer is OK
  43.  
  44.   /Palette1 <ff 00> def
  45.   /Palette8
  46. %========== Here we define all 256 color entries. ======
  47. %========== Colors were taken from the X Windows default colors, ======
  48. %========== hacked up a little so we get all 8 primaries. ======
  49. <
  50. 000000 ffffff a8a8a8 ebebeb 5c5c5c 373737 5f929e 85ccdd
  51. 345057 1f3034 729efe a0ddff 3f578c 263454 6186d8 2f2f64
  52. ffff00 b0e2ff ff0000 808080 efdf84 55fe55 fe0000 66fe57
  53. fe987a feca71 fefefe fe8d7c fea977 fec472 feb875 fe937b
  54. fe957a feb575 98fe5d fe8b7d fea677 feb276 feaf76 febe74
  55. fe837e fefc6a fe0c9a fe0350 fe08b5 6715fe fefb3f fe544b
  56. 00c000 20c000 40c000 60c000 80c000 a0c000 c0c000 e0c000
  57. 00ff00 20e000 40e000 60e000 80e000 a0e000 c0e000 e0e000
  58. 000040 200040 400040 600040 800040 a00040 c00040 e00040
  59. 002040 202040 402040 602040 802040 a02040 c02040 e02040
  60. 004040 204040 404040 604040 804040 a04040 c04040 e04040
  61. 006040 206040 406040 606040 806040 a06040 c06040 e06040
  62. 008040 208040 408040 608040 808040 a08040 c08040 e08040
  63. 00a040 20a040 40a040 60a040 80a040 a0a040 c0a040 e0a040
  64. 00c040 20c040 40c040 60c040 80c040 a0c040 c0c040 e0c040
  65. 00e040 20e040 40e040 60e040 80e040 a0e040 c0e040 e0e040
  66. 000080 200080 400080 600080 800080 a00080 c00080 e00080
  67. 002080 202080 402080 602080 802080 a02080 c02080 e02080
  68. 004080 204080 404080 604080 804080 a04080 c04080 e04080
  69. 006080 206080 406080 606080 806080 a06080 c06080 e06080
  70. 008080 208080 408080 608080 808080 a08080 c08080 e08080
  71. 00a080 20a080 40a080 60a080 80a080 a0a080 c0a080 e0a080
  72. 00c080 20c080 40c080 60c080 80c080 a0c080 c0c080 e0c080
  73. 00e080 20e080 40e080 60e080 80e080 a0e080 c0e080 e0e080
  74. 0000ff 2000c0 4000c0 6000c0 8000c0 a000c0 c000c0 ff00ff
  75. 0020c0 2020c0 4020c0 6020c0 8020c0 a020c0 c020c0 e020c0
  76. 0040c0 2040c0 4040c0 6040c0 8040c0 a040c0 c040c0 e040c0
  77. 0060c0 2060c0 4060c0 6060c0 8060c0 a060c0 c060c0 e060c0
  78. 0080c0 2080c0 4080c0 6080c0 8080c0 a080c0 c080c0 e080c0
  79. 00a0c0 20a0c0 40a0c0 60a0c0 80a0c0 a0a0c0 c0a0c0 e0a0c0
  80. 00c0c0 20c0c0 40c0c0 60c0c0 80c0c0 a0c0c0 c0c0c0 e0c0c0
  81. 00ffff 20e0c0 40e0c0 60e0c0 80e0c0 a0e0c0 c0e0c0 e0e0c0
  82. > def
  83.  
  84.   % Define a procedure for computing the output file name for a given page.
  85.   /pagefilename        % <int|null> pagefilename -> <string
  86.    { OutFilePrefix FileName concatstrings (.) concatstrings
  87.      exch dup null ne
  88.       { 1 sub FirstPageNumber add (xxxxxx) cvs concatstrings }
  89.       { pop }
  90.      ifelse (ppm) concatstrings
  91.    } bind def
  92.  
  93.   % Redefine copypage and showpage appropriately.
  94.   /copypage
  95.    { ppmdict begin
  96.  
  97.        /PageCount PageCountString cvi 100000 sub def
  98.        /PageCount PageCount 1 add def
  99.  
  100.        PageCount 1 eq
  101.     { null pagefilename            % first page
  102.     }
  103.     { PageCount 2 eq
  104.        {                    % second page, rename first
  105.          null pagefilename 1 pagefilename
  106.          (Renaming ) print 1 index print ( to ) print
  107.            dup print (\n) print
  108.          flush
  109.          renamefile
  110.        } if
  111.       PageCount pagefilename
  112.     } ifelse
  113.        /FileNameOut exch def
  114.  
  115.        FileNameOut (w) file
  116.          /FileDescOut exch def
  117.  
  118.        (Writing ) print FileNameOut print (\n) print
  119.        flush
  120.        FileDescOut Device writeppmfile
  121.        FileDescOut closefile
  122.  
  123.        WrotePage 0 1 put    % /WrotePage true def
  124.        PageCount 100000 add PageCountString cvs pop
  125.  
  126.      end
  127.    } bind userdict begin odef end
  128.   /showpage
  129.    { copypage erasepage initgraphics
  130.    } bind userdict begin odef end
  131.  
  132.   % Redefine grestoreall so it doesn't undo the device selection
  133.   /grestoreall /grestoreall load def
  134.   /grestoreall
  135.    { ppmdict begin   grestoreall   Device setdevice   end
  136.    } bind userdict begin odef end
  137.  
  138.   /Convert            % the main procedure
  139.                   % <filename> <palette> Convert -
  140.    { /Palette exch def
  141.      /FileName exch def
  142.  
  143.      % Save and restore don't save and restore the contents of strings.
  144.      % Therefore, we use strings to hold the two variables whose values
  145.      % must persist across page boundaries (PageCount and WrotePage).
  146.  
  147.      /PageCountString 6 string def
  148.      100000 PageCountString cvs pop
  149.      /WrotePage 1 string def
  150.  
  151.      /ScaleX Horz_dpi 72 div def
  152.      /ScaleY Vert_dpi 72 div def
  153.  
  154.      /Width 85 Horz_dpi mul 5 add 10 div cvi def % add 5 to round up!
  155.      /Height 11 Vert_dpi mul def
  156.  
  157.      FileName (.ps) concatstrings
  158.        /FileNameIn exch def        % file name with extension
  159.  
  160.      [ScaleX 0.0 0.0 ScaleY neg 0.0 Height]
  161.      Width Height Palette makeimagedevice
  162.        /Device exch def
  163.      Device setdevice
  164.  
  165.      % For running the file, remove ppmdict from the dict stack
  166.      FileNameIn end run        % ppmdict
  167.      ppmdict begin
  168.      WrotePage 0 get 0 eq { showpage } if % make sure the page got written
  169.      end            % ppmdict
  170.  
  171.    } bind def
  172.  
  173. end                % ppmdict
  174.  
  175. %%%%%% Define the user-callable procedures
  176.  
  177. /ppmsetdensity
  178.  { ppmdict begin
  179.    /Vert_dpi exch def
  180.    /Horz_dpi exch def
  181.    end
  182.  } bind def
  183.  
  184. /ppmsetprefix
  185.  { ppmdict begin
  186.    /OutFilePrefix exch def
  187.    end
  188.  } bind def
  189.  
  190. /ppmsetfirstpagenumber
  191.  { cvi
  192.    ppmdict begin
  193.    /FirstPageNumber exch def
  194.  } bind def
  195.  
  196. /ppm1run
  197.  { ppmdict begin   Palette1 Convert
  198.  } bind def
  199.  
  200. /ppm8run
  201.  { ppmdict begin   Palette8 Convert
  202.  } bind def
  203.  
  204. /ppm24run
  205.  { ppmdict begin   null Convert
  206.  } bind def
  207.  
  208. %%%%%% Display instructions for the user.
  209.  
  210. (Usage: (file) ppmNrun\n) print
  211. (   converts file.ps to file.ppm (single page),\n) print
  212. (     or file.1ppm, file.2ppm, ... (multi page).\n) print
  213. (   N is # of bits per pixel (1, 8, or 24).\n) print
  214. (Examples: (golfer) ppm1run   ..or..   (escher) ppm8run\n) print
  215. (Optional commands you can give first:\n) print
  216. (   horiz_DPI vert_DPI ppmsetdensity\n) print
  217. (   (dirname/) ppmsetprefix\n) print
  218. (   page_num ppmsetfirstpagenumber\n) print
  219. flush
  220.